package com.bonc.ioc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.service.impl.CollectionServiceImpl;
import com.bonc.ioc.service.impl.StatisticsServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhangXu
 * @date 2021/3/31 15:15
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    private static final Logger log = LoggerFactory.getLogger(CollectionServiceImpl.class);

    @Autowired
    private StatisticsServiceImpl statisticsService;

    @GetMapping("/queryStatistics")
    public AppReply queryStatistics(Integer type){
        log.info("统计 查询统计类型={}", JSON.toJSONString(type));
        JSONObject result = new JSONObject();
        switch (type) {
            case 0 :
            //数据来源统计
            case 1 :
                result.putAll(statisticsService.queryDataSources());
                if (type!=0){ break; }
            //学科统计
            case 2 :
                result.putAll(statisticsService.querySubjects());
                if (type!=0){ break; }
            //年度增量
            case 3 :
                result.putAll(statisticsService.queryAnnualIncrement());
                if (type!=0){ break; }
            //月度增量
            case 4 :
                result.putAll(statisticsService.queryMonthlyIncrement());
                if (type!=0){ break; }
            //资源总量
            case 5 :
                result.putAll(statisticsService.queryResourcesTotal());
                if (type!=0){ break; }
            //文献数量
            case 6 :
                result.putAll(statisticsService.queryDocumentsTotal());
                if (type!=0){ break; }
            //最常用数据
            case 7 :
                result.putAll(statisticsService.queryCommonData());
                if (type!=0){ break; }
            //下载数量
            case 8 :
                result.putAll(statisticsService.queryMonthlyDownloads());
                break;
            default:
                return new AppReply(AppReply.EORRO_CODE,"查询失败，无此统计类型",result);
        }
        return new AppReply(AppReply.SUCCESS_CODE,"查询成功",result);
    }

    @GetMapping("/queryRanking")
    public AppReply queryRanking(Integer type){
        log.info("统计 查询排行类型={}", JSON.toJSONString(type));
        JSONObject result = new JSONObject();
        switch (type) {
            //所有排行榜
            case 0 :
            //最新发布数据
            case 1 :
                result.putAll(statisticsService.queryDataLatestReleasedRanking());
                if (type!=0){ break; }
            //数据下载排行
            case 2 :
                result.putAll(statisticsService.queryDataDownloadsRanking());
                if (type!=0){ break; }
            //数据收藏排行
            case 3 :
                result.putAll(statisticsService.queryDataCollectionRanking());
                if (type!=0){ break; }
            //数据点击排行
            case 4 :
                result.putAll(statisticsService.queryDataViewsRanking());
                break;
            default:
                return new AppReply(AppReply.EORRO_CODE,"查询失败，无此排行类型",result);
        }
        return new AppReply(AppReply.SUCCESS_CODE,"查询成功",result);
    }
}
